package com.wxzz.elearing.comment.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.*;
import com.wxzz.elearing.comment.entity.Comment;
import com.wxzz.elearing.comment.entity.CommunityComment;
import com.wxzz.elearing.comment.service.ICommentService;
import com.wxzz.elearing.comment.service.ICommunityCommentService;
import com.wxzz.elearing.comment.vo.AnswersVo;
import com.wxzz.elearing.comment.vo.CommunityAnswersVo;
import com.wxzz.elearing.comment.vo.CommunityQuestionsContentDescVo;
import com.wxzz.elearing.comment.vo.QuestionsContentDescVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wx-comment-community")
@Api(tags = "社区评论接口(小程序)")
public class WxCommunityCommentController {

    @Resource
    private ICommunityCommentService communityCommentService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = "社区评论(小程序)", notes = "社区评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveComment", method = RequestMethod.POST)
    public ReturnVO saveComment(@RequestBody CommunityComment comment, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        comment.setIcon(jsonObject.getString("icon"));
        comment.setNickname(jsonObject.getString("nickname"));
        comment.setWxUserId(jsonObject.getLong("id"));
//        comment.setNickname("翟越宸");
//        comment.setIcon("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKa1W1FQ4AwGCerPtVffQZuPHXbcofM28l1kReIyHvdMicBxGKYrdhHPHiaY0wqjfVK0AapVoHslicKQ/132");
//        comment.setWxUserId(1392071664227999745L);
        return communityCommentService.saveComment(comment);
    }

    @ApiOperation(value = "社区根据文章id获取详情(小程序)", notes = "社区根据文章id获取详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listAllComment/{articleId}", method = RequestMethod.POST)
    public ReturnVO<CommunityQuestionsContentDescVo> listAllComment(@PathVariable Long articleId) {
        return communityCommentService.listAllComment(articleId);
    }

    @ApiOperation(value = "社区根据文章id列出所有评论", notes = "社区根据文章id列出所有评论", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "answersDescList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<CommunityComment>>> answersDescList(@RequestBody CommunityAnswersDescDto answersDescDto) {
        return communityCommentService.answersDescList(answersDescDto);
    }

    @ApiOperation(value = "查询个人所有评论", notes = "查询个人所有评论", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "answersList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<CommunityAnswersVo>>> getAnswersList(@RequestBody CommunityAnswersDto answersDto) {
        return communityCommentService.getAnswersList(answersDto);
    }

    @ApiOperation(value = "社区采纳评论", notes = "社区采纳评论", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "setAccept", method = RequestMethod.POST)
    public ReturnVO setAccept(@RequestBody CommunityQuestionsAnswersDto questionsAnswersDto) {
        return communityCommentService.setAccept(questionsAnswersDto);
    }

}
